//
//  GHSStockTableViewCell.m
//  penzer
//
//  Created by 还带大道 on 2018/11/20.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GHSStockTableViewCell.h"
#import "GHSGoodsModel.h"

@interface  GHSStockTableViewCell()

@property (weak, nonatomic) IBOutlet UIView *content_bgV;
@property (weak, nonatomic) IBOutlet UIView *image_bgV;
@property (weak, nonatomic) IBOutlet UIView *tagsView;
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *price1;
@property (weak, nonatomic) IBOutlet UILabel *price2;
@property (weak, nonatomic) IBOutlet UILabel *number;
@property (weak, nonatomic) IBOutlet UILabel *goodsNumber;
@property (nonatomic,strong) NSMutableArray<UILabel *> *tagItems;
@property (weak, nonatomic) IBOutlet UIButton *selectBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentV_rightMarginSup;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentV_leftMarginSup;


@end
@implementation GHSStockTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    //设置阴影
    _content_bgV.backgroundColor = [UIColor whiteColor];
    _content_bgV.layer.shadowColor = RZ_COLOR_ALPHA(0, 0, 0, 0.1).CGColor;
    _content_bgV.layer.cornerRadius = 2.5;
    _content_bgV.layer.shadowOffset = CGSizeMake(0,1);
    _content_bgV.layer.shadowOpacity = 1;
    _content_bgV.layer.shadowRadius = 5;
    
    _image_bgV.backgroundColor = [UIColor whiteColor];
    _image_bgV.layer.borderWidth = 0.5;
    _image_bgV.layer.borderColor = RZ_COLOR(214, 214, 214).CGColor;
    _image_bgV.layer.cornerRadius = 5;
    
    _selectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    
    [self addTags];
    
    _title.text = @"新款iPad 银色 WiFi版 32G";
    _number.text = @"库存：300";
    self.tagsView.backgroundColor = [UIColor whiteColor];
    
}


- (void)setModel:(GHSGoodsModel *)model
{
    _model = model;
    _contentV_leftMarginSup.constant = model.isEdit?40:15;
    _contentV_rightMarginSup.constant = model.isEdit?-25:15;
    _selectBtn.selected = model.isSelected;
    _selectBtn.hidden = !model.isEdit;
}






- (void)addTags
{
    NSArray<NSString *> *items = @[@"星迹版",@"双卡双待"];
    [items enumerateObjectsUsingBlock:^(NSString * _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *label = [UILabel new];
        [self.tagsView addSubview:label];
        label.text = str;
        label.textColor = RZ_COLOR(74, 74, 74);
        label.font = [UIFont fontWithName:@"PingFangSC-Light" size:12];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = RZ_COLOR(241, 241, 241);
        label.layer.cornerRadius = 2;
        label.clipsToBounds = YES;
        [self.tagItems addObject:label];
    }];
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self setTagsViewFrame];
    [self layoutIfNeeded];
    self.content_bgV.layer.shadowPath = [UIBezierPath bezierPathWithRect:_content_bgV.bounds].CGPath;
    self.image_bgV.layer.shadowPath = [UIBezierPath bezierPathWithRect:_image_bgV.bounds].CGPath;
}


- (void)setTagsViewFrame
{
    [self.tagsView layoutIfNeeded];
    CGSize size = self.tagsView.size;
    __block CGFloat x = 0;
    __block CGFloat y = 0;
    __block CGFloat width = 0;
    CGFloat height = 23;
    CGFloat margin = 5;
    [self.tagItems enumerateObjectsUsingBlock:^(UILabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [label sizeToFit];
        width = label.width+10;
        if(idx == 0){
            x = 0;
            y = 0;
            
        }else{
            
            x = CGRectGetMaxX(self.tagItems[idx-1].frame)+margin;
            if(x + width > size.width){//换行
                x = 0;
                y = CGRectGetMaxY(self.tagItems[idx-1].frame) + margin;
            }else{
                y = CGRectGetMinY(self.tagItems[idx-1].frame);
            }
        }
        
        label.frame = CGRectMake(x, y,width,height);
    }];
}



- (IBAction)selectBtnOnClick:(UIButton *)btn{
    btn.selected = !btn.isSelected;
    _model.selected = btn.isSelected;
}



#pragma mark--setter&&getter
- (NSMutableArray<UILabel *> *)tagItems
{
    if(!_tagItems){
        _tagItems = [NSMutableArray array];
    }
    return _tagItems;
}


@end
